<?php
session_start();
$img_width=50;
$img_height=20;
srand(microtime() * 100000);
$_SESSION["s_verify"]=randStr();
$number_img=imageCreate($img_width,$img_height);
ImageColorAllocate($number_img,255,255,255);
for($i=1;$i<=128;$i++){
	imageString($number_img,1,mt_rand(1,$img_width),mt_rand(1,$img_height),"*",imageColorAllocate($number_img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)));
}
for($i=0;$i<strlen($_SESSION["s_verify"]);$i++){
	imageString($number_img,mt_rand(3,5),$i*$img_width/4+mt_rand(2,4),mt_rand(1,$img_height/8), $_SESSION["s_verify"][$i],imageColorAllocate($number_img,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)));
}
//$borderColor = ImageColorAllocate($number_img, 0, 0, 0);//边框色 
//@imagerectangle($number_img, 0, 0, $img_width-1, $img_height-1, $borderColor); //边框位置 
@imagerectangle($number_img, 0, 0, $img_width-1, $img_height-1); //边框位置 
header("Content-type: image/png");
ImagePng($number_img);
ImageDestroy($number_img);


/**
 * 功能：产生随机字符串函数
 * 参数：$len 随机数的个数； $format 随机数的格式
**/
function randStr($len=4,$format='ALL') { 
	switch($format) { 
	case 'ALL': 
		$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; break; 
	case 'CHAR': 
		$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; break; 
	case 'NUMBER': 
		$chars='0123456789'; break; 
	default : 
		$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; 
	break; 
	} 
	$string=""; 
	while(strlen($string)<$len) 
		$string.=substr($chars,(mt_rand(0,strlen($chars))),1); 
	return $string; 
} 
?>